﻿using ProjectManager.Interface;
using ProjectManager.Logic.Content;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using LibGit2Sharp;
using ProjectManager.Logic;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace ProjectManager.Controller
{
    internal partial class LogicController : ILogic
    {
        // 定时执行拉取最新分支
        private readonly System.Threading.Timer timer;
        private ProjectInformation? currentProject;
        private MemberInformation? currentIdentity;
        public ProjectInformation CurrentProject
        {
            get => this.currentProject ?? new();
            set => this.currentProject = value;
        }
        public MemberInformation CurrentIdentity
        {
            get => this.currentIdentity ?? new();
            set => this.currentIdentity = value;
        }

        private readonly IGit gitController = new GitController();
        private readonly ILocalFile localFileController = new LocalFileController();
        private readonly ISql sqlController = new SqlController();

        private Action<string>? ShowWait;
        private Action? EndWait;
        private Action<string>? ShowErrorMessage;

        public LogicController()
        {
            var userConfig = localFileController.GetUserConfig();
            this.currentProject = userConfig?.CurrentProject;
            this.currentIdentity = userConfig?.CurrentIdenity;
            timer = new System.Threading.Timer(CallBackMethod, null, 0, 10 * 1000);
        }
        public void InitUserAction(Action<string> ShowWait, Action EndWait, Action<string> ShowErrorMessage)
        {
            this.ShowWait = ShowWait;
            this.EndWait = EndWait;
            this.ShowErrorMessage = ShowErrorMessage;
        }
        public List<string> CheckMessages()
        {
            if (this.currentProject == null) return [];
            var taskMessages = gitController.GetCommitMessages(CurrentProject.TaskPath, "Task");
            var memberMessages = gitController.GetCommitMessages(CurrentProject.MemberPath, "Member");
            var result = new List<string>();
            result.AddRange(taskMessages);
            result.AddRange(memberMessages);
            return result;
        }
    }
}
